Move the common application code to a separated file

Move common code to facilitate the creation of new applications. Move
common methods and events.