Cleaned up JavaScript to make it more extensible