General re-structuring and cleaning of javascript code