re-arranged javascript