made code less wet and various improvements