Added tests for edge cases and tidied