Added comprehensive testing and fixed a small bug