Added tests and refactored collision handling.