We present surface hopping dynamics on potential energy surfaces resulting from the spin-orbit splitting, i.e., surfaces corresponding to the eigenstates of the total electronic Hamiltonian including the spin-orbit coupling. In this approach, difficulties arise because of random phases of degenerate eigenvectors and possibility of crossings of the resulting mixed states. Our implementation solves these problems and allows propagation of the coefficients both in the representation of the spin free Hamiltonian and directly in the "diagonal representation" of the mixed states. We also provide a detailed discussion of the state crossing and point out several peculiarities that were not mentioned in the previous literature. We also incorporate the effect of the environment via the quantum mechanics/molecular mechanics approach. As a test case, we apply our methodology to deactivation of thiophene and selenophene in the gas phase, ethanol solution, and bulk liquid phase. First, 100 trajectories without spin-orbit coupling have been calculated for thiophene starting both in S1 and S2 states. A subset of 32 initial conditions starting in the S2 state was then used for gas phase simulations with spin-orbit coupling utilizing the 3-step integrator of SHARC, our implementation of the 3-step propagator in Newton-X and two new "one-step" approaches. Subsequently, we carried out simulations in ethanol solution and bulk liquid phase for both thiophene and selenophene. For both molecules, the deactivation of the S2 state proceeds via the ring opening pathway. The total population of triplet states reaches around 15% and 40% after 80 fs for thiophene and selenophene, respectively. However, it only begins growing after the ring opening is initiated; hence, the triplet states do not directly contribute to the deactivation mechanism. For thiophene, the resulting deactivation lifetime of the S2 state was 68 fs in the gas phase, 76 fs in ethanol solution, and 78 fs in the liquid phase, in a good agreement with the experimental value of 80 fs (liquid phase). For selenophene, the obtained S2 lifetime was 60 fs in the gas phase and 62 fs for both ethanol solution and liquid phase. The higher rate of intersystem crossing to the triplet states in selenophene is likely the reason for the lower fluorescence observed in selenium containing polymer compounds.