Implementing code provided by jimmydafish of xda-developers.com.