Import PR #43 as base (refactor)