refactor main to use metadata. Fully operational