Cleaned up, optimized code and added Javadocs