refactored main procedure