Adding tests and fixing code based on results