most of the code moved outside of .ready