Better code structure, small fixes