adding first/last, refactoring and bug fixes