Refactored for pi use