code optimizations; hopefully runs faster (NEED TO TEST!!!)