reorganize the code so that it works as a drop-in library for arduino too