Cleaned up code a bit.
Added some javadocs.