Solution with some parts via Mr.Brown