Cleaned up functions (only depend on their arguments) and moved code populating position and adjacency arrays into functions