Fix bug where end and begin were mixed up