Fixed function bugs