Relocate code to allow using this code as a library