Handling end correctly