Functionalize the code a bit
Refactor JS codes nicely