Refactor rpc and mnesia related codes