Fixing bug in function application and adding tests to cover this situation