Better implementation of 056d6847c0d9c696d83f6205e68cedfa1c4d225a