refactored js and various optimizations