Hack fixed #443 and added appropriate tests - requires refactor.