refactored master with wisdom from pitft
made it work on device