Some refactoring has been made. Lines checking has been added