Cleaned up code. Minor optimizations