Cleaned up code. Greatly reduced the number of datastore operations.