Refactor the code to make it possible to embed in other projects