Cleaned up code and preformed minor optimizations