Made performance fixes, simplified some of the code