8-puzzle - Functions not working correctly