Refactored things a bit, added comments and such