More modularity, moved all function to a library