Cleaned up javascript significantly. Added additional documentation.