Fixed numerous memory issues and silly bugs