move code to lib directory and split into separate files