fixed mistakes and added test code