Cleaned up javascript a little by creating some helper functions.