Code completely refactored; it is now ready to integrate with game code.
test?