implemented changes for performance gains suggested by kmsquire
v2 after implementing changes suggested by kmsquire