Rewritten to simplify some things and to deals with 3.10.17 kernel.