major performance improvements and more DRY code