fixed bugs and improvements
Added test