added resource pooling, refactored a bit