refactored by nerdmed and rikner