Corrected bugs and added test