fix #169. Corrected the bug introduced by previous commit. Performed some code redundancies cleanup in the meantime.