Fixed an issue with first function- works correctly now