Fixes #976
Clean up code