move reader function to separate file, fix some issues