ugly but functional a* pathfinding