standalone, refactored per anshul_s suggestions