Refactored from [here](https://github.com/mortardata/Lipstick/pull/3/files#L3L193)