Rewritten to make more autonomous