clearer code and added unittest