Refactored javascript a bit!