Refactoring and setting up learn mode (UNSTABLE: Beware this commit)