Make mappings buffer-local and refactor functions
Fix calls to local functions and fix toggle bug