complete, not refactored version