Tidy up of code, moved all function inside jQery(document).ready()