small optimizations and tweaks
added test case