Code reduced with minor fixes