Added test case, better code modulation, more readable code