Made code more efficient by making changes that mike suggested on pullrequest