Added debugging and optimizations