ENH added test and cleaned up code in lib