changed acrchitecture of js code

still not sure if this is right way to do :P