Cleaned up code, added javadocs