Added power cycling code & optimised. needs testing