Fixed unbound scoped closure edge cases and added tests for them