Some small bug fixes and optimizations.
Merge pull request #1 from TheUnknownProgrammer/master

Optimized code and added little bug fixes.