removed unnseecary code, compiled all good code into efficient loop