Cleaned up old code and added comments