slightly better utility function and some more optimizations!