Major refactoring; fixes bugs, improves reliability and manages audio gain and loss