Refactored JS, and adhered to ruby code standard