added more logic;

removed unused code;