JS code partially refactored

- introduced namespaces
- introduces IIFEs with defensive semicolons
- moved code
- deleted redundant code. Because one is enough.