cleaned up the code with comments and optimizations, final working build