Javascript overhaul: Fixed several js errors