Improved code structure, added some javadoc