Refactoring starting and stopping functions
Add some tests