Added tests and revised both solutions to address errors.