Moved general code in separate file.