some optimizations, cleaned up some code