DRY up code
DRY up and refactor javascript