Cleaned up javascript and fixed logic error