updated code to match mag instructions, tested successfully