Added some comments and cleaned up separation of concerns a little