added: more tests, optimizations and incrporating some of @mdebbar ideas