Rewritten with better style and fixes pip method