Fix three errors and firs refact