Further refactoring of service and main activity

More refactoring.